package crawlertest.crawler;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Entities.EscapeMode;
import org.jsoup.select.Elements;

public class CThread implements Runnable
{

	int num;
	boolean wait;
	ReentrantLock lock1;	//4workurls
	Condition empty;
	ReentrantLock lock2;	//4avisit
	ReentrantLock lock3;	//4catch e
	ReentrantLock lock4;	//4w file
	String str;
	public CThread(int num,ReentrantLock lock1,
			Condition empty,ReentrantLock lock2
			,ReentrantLock lock3,ReentrantLock lock4) {
		// TODO Auto-generated constructor stub
		this.num=num;
		this.lock1=lock1;
		this.empty=empty;
		this.lock2=lock2;
		this.lock3=lock3;
		this.lock4=lock4;
	}
	@Override
	public void run() 
	{
		// TODO Auto-generated method stub
		while(ShareVars.finish==0)
		{
			lock1.lock();
			while(ShareVars.workurls.isEmpty()==true)
			{
				try {
					empty.await();
					
					if(ShareVars.finish==1)
					{		
						System.out.println("线程"+this.num+"Signal结束");
						lock1.unlock();
						return;
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}
			}
			str=ShareVars.workurls.poll();
			
			lock1.unlock();
			if(str==null)
				return;
			try {
				//connect
				URL url=new URL(str);
				HttpURLConnection con=(HttpURLConnection) url.openConnection();		
				con.setConnectTimeout(5000);
				con.setReadTimeout(5000);
				con.connect();
				
				//save file
				BufferedInputStream bi=new BufferedInputStream(con.getInputStream(),8192);
				byte[] buf=new byte[8192];
				
				lock4.lock();
				String tmpstr1="c:"+File.separator+"Htmls"+File.separator+String.valueOf(ShareVars.count)+".html";
				File file=new File(tmpstr1);
				ShareVars.count++;
				lock4.unlock();
				
			
				if(file.exists()==false)
					file.createNewFile();
				
				
				BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream(file), 8192);
				
				int result;
				
				while(true)
				{
					
					result=bi.read(buf);
					if(result==-1)
						break;
					bo.write(buf);
				}
				
				bi.close();
				bo.close();
				//parse HTML
				Document doc = Jsoup.parse(file,null);
				
				Elements links = doc.getElementsByTag("a");
				lock2.lock();
				lock1.lock();
				for (Element link : links) 
				{
				 String l=link.attr("abs:href");
				 if(ShareVars.avisit.contains(l))
					 continue;
				 else
				 {
					 ShareVars.avisit.add(l);
					 ShareVars.workurls.offer(l);
				 }
				}
				empty.signalAll();
				lock1.unlock();
				lock2.unlock();
				
				
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				lock3.lock();
				int n=ShareVars.num;
				
				try {
					ShareVars.es.execute(new CThread(n,ShareVars.lock1,ShareVars.empty,
							ShareVars.lock2,ShareVars.lock3,ShareVars.lock4));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					lock3.unlock();
				//	e1.printStackTrace();
				}
				ShareVars.num+=1;
				try {
					lock3.unlock();
				} catch (Exception e2) {
					// TODO: handle exception
				}
				
			}			
		}//while
	}//run

}
